package com.example.dbmysql.Controller;


import com.example.dbmysql.pojo.NowStay;
import com.example.dbmysql.pojo.RoomHistory;
import com.example.dbmysql.pojo.RoomList;
import com.example.dbmysql.result.Result;
import com.example.dbmysql.service.NowStayService;
import com.example.dbmysql.service.RoomListService;
import com.example.dbmysql.service.roomHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roomHistory")
public class roomHistoryController {

    @Autowired
    private roomHistoryService roomHistoryService;

    @Autowired
    private RoomListService roomListService;

    @Autowired
    private NowStayService nowStayService;

    @GetMapping("/")
    public Result get(){

        return  Result.success();
    }


    @PostMapping("/changeIn")
    public  Result changeIn(@RequestBody RoomHistory roomHistory){
        NowStay nowStay = nowStayService.selectByRoomId(roomHistory.getRoomId());
//      List<NowStay> list  = nowStayService.selectAll();
        System.out.println(nowStay);

        if(nowStay == null){



            System.out.println("还未入住添加");

            roomHistoryService.NewRecording(roomHistory);

            roomListService.updateStatusByRoomName(roomHistory.getRoomName(),2);

            nowStayService.NewRecording(roomHistory);

        }else{
            // 如果两个卡号相等 表明当前用户向后延期更新当前用户的离店时间
            if(nowStay.getCardNum().equals(roomHistory.getCardNum())){
                // 如果卡号相等房号不相等 表明更换房间
                if(nowStay.getRoomName().equals(roomHistory.getRoomName())){

                    nowStayService.updateTime(roomHistory.getDepartureTime());

                }else{
                    // 房号不同 先退原来的房间 然后新增一共发卡记录
                    roomListService.updateStatusByRoomName(roomHistory.getRoomName(),2);
                    nowStayService.updateRoomName(roomHistory.getRoomName());
                    roomHistoryService.returnRoomByRoomName(roomHistory);
                    roomHistoryService.NewRecording(roomHistory);
                }

            }else {
                // 如果不相等 则表示 一个房间内发多张卡
                nowStayService.NewRecording(roomHistory);

            }
        }

        return  Result.success();
    }


}